<template>
	<view class="container">
		<uni-card>
			<view class="book-info">
				<image :src="img" style="width:60%;"></image>
				<view class="bookname">{{getData.bookName}}</view>
				<view class="author">{{getData.author}}</view>
			</view>
			<view class="book-evaluate">
				<ratebox :ratedata="comment"></ratebox>
				<!-- <view class="bottom-box">
					<view class="bottom-box-item" style="border-right:1px solid #F2F2F4">
						<text>阅读</text>
						<text><text style="font-size:42rpx;">{{getData.totalReadCount / 10000 || 0}}</text><text style="font-size:22rpx;">万人</text></text>
					</view>
					<view class="bottom-box-item" style="border-right:1px solid #F2F2F4;color:#CBA36A;font-weight:bold;">
						<text>VIP</text>
						<text>会员卡可读</text>
					</view>
					<view class="bottom-box-item">
						<text>字数</text>
						<text><text style="font-size:42rpx;">{{getData.totalWords / 10000|| 0}}</text><text style="font-size:22rpx;">万字</text></text>
					</view>
				</view> -->
			</view>
			<view class="btn">
				<text  size="large" @click="look"
					>开始阅读</text>
			</view>
		</uni-card>
	</view>
</template>

<script setup>
import { onMounted, ref } from "vue";
import useGetGlobalProperties from '@/util/useGlobal.js'
import ratebox from '@/components/rate/rate.vue'
import {
		onLoad
	} from '@dcloudio/uni-app'
//const url = useGetGlobalProperties().$localHttp;
const url = useGetGlobalProperties().$https;
	const data = ref();
	const getData = ref();
	const comment = ref()
	const img = ref()
	const fileUrl = ref()
	const files = ref()
	onLoad((option) => {
		getData.value = option.id;
	});
	onMounted(() => {
		init(getData.value)
	});
	const init=(e)=>{
		uni.showLoading({
			title:'正在加载中'
		})
		// let urlFile = uni.getStorageSync('bookFileUrl');
		uni.request({
			url:url+'/homepage/' + e,
			method:'GET',
			success: (res) => {
				getData.value = res.data.data.data;
				comment.value = res.data.data
				img.value = getData.value.coverPictures[0].coverPictureUrl;
				fileUrl.value = getData.value.file[0].fileUrl
				files.value = getData.value.file[0]
				uni.hideLoading()
			}
		})
	}
	const look = () => {
		if(files.value.type=='epub'){
			uni.setStorageSync('bookFileUrl',fileUrl.value)
			uni.navigateTo({
				url:'/pages/readBook/readBook'
			})
		}else{
			let code = '';
			uni.showLoading({
				title:'正在加载中'
			})
			uni.login({
				provider: 'weixin',
				onlyAuthorize: true,
				success: res => {
					console.log(res, '登录成功')
					code = res.code;
					console.log("code=" + res.code);
					uni.request({
						// url:  'https://weixin.shuzinongyue.com:8942/userInfo/getOpenId/' + code,
						// url:  'http://61.163.243.13:8944/userInfo/getOpenId/' + code,
						url:  'https://wechat.shuzinongyue.com:8922/userInfo/getOpenId/' + code,
						method: 'GET',
						success: (res) => {
							if (res.data.data) {
								console.log('res.data.look', res.data.data)
								console.log('fileUrl.value',fileUrl.value)
								uni.setStorageSync('userInfo', res.data.data);
								wx.downloadFile({
									url: fileUrl.value,
									success: function(res) {
										console.log('res', res);
										var filePath = res.tempFilePath;
										wx.openDocument({
											filePath: filePath,
											fileType: 'pdf',
											showMenu: true,
											success: function(res) {
												uni.hideLoading()
												console.log('打开文档成功');
											}
										});
									}
								});
								return;
							} else {
								uni.showToast({
									title: '请登录后进行阅读',
									icon: 'success',
									duration: 1500,
									mask: false
								});
								uni.redirectTo({
									// url: '/pages/admin/login'
								})
							}
						}
					})
				}
			})
		}
		uni.navigateTo({
			url:'/pages/readBook/readBook'
		})
		// uni.navigateTo({
		//       url: '/pages/index/webview/webview?url=' +encodeURIComponent('https://zhongtai.zhongjiaoyuntu.com:9002/dzyx/%E7%9B%9B%E5%A4%8F/%E7%9B%9B%E5%A4%8F.html')
		//     });
		
	}
</script>

<style>
	@import url("./index.css");
</style>